﻿using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class LoHangService : System.Web.Services.WebService
{
    public LoHangService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    [WebMethod]
    public DTO_SanPham[] LayLoHang()
    {
        string strconnection = ConfigurationManager.ConnectionStrings["strconnection"].ToString();
        SqlConnection conn = new SqlConnection(strconnection);
        SqlCommand comm = new SqlCommand();
        comm.CommandText = "select * from SANPHAM";
        comm.Connection = conn;
        DataTable table = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = comm;
        adapter.Fill(table);
        DTO_SanPham[] arrSanPham = new DTO_SanPham[table.Rows.Count];
        for (int i = 0; i < table.Rows.Count; i++)
        {
            DTO_SanPham temp = new DTO_SanPham();
            temp.MaSP = table.Rows[i]["MaSP"].ToString();
            temp.TenSP = table.Rows[i]["TenSP"].ToString();
            temp.ThongTinChiTiet = table.Rows[i]["ThongTinChiTiet"].ToString();
            temp.HanSuDung = (DateTime)table.Rows[i]["HanSuDung"];
            temp.SoLuong = (int)table.Rows[i]["SoLuong"];
            temp.DonGia = (decimal)table.Rows[i]["DonGia"];
            temp.ChietKhau = (int)table.Rows[i]["ChietKhau"];
            arrSanPham[i] = temp;
        }
        conn.Close();
        return arrSanPham;
    }

    [WebMethod]
    public int ThuongLuong(string masp, int soluong, decimal dongia, int chietkhau)
    {
        string strconnection = ConfigurationManager.ConnectionStrings["strconnection"].ToString();
        SqlConnection conn = new SqlConnection(strconnection);
        SqlCommand comm = new SqlCommand();
        comm.CommandText = "select * from SANPHAM where MaSP='"+masp+"'";
        comm.Connection = conn;
        DataTable table = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = comm;
        adapter.Fill(table);
        conn.Close();
        int soluongcu = (int)table.Rows[0]["SoLuong"];
        decimal dongiacu = (decimal)table.Rows[0]["DonGia"];
        int chietkhaucu = (int)table.Rows[0]["ChietKhau"];
        if (Math.Abs(soluong - soluongcu) > 10)
            return 0;
        if (Math.Abs(dongia - dongiacu) > 10000)
            return 0;
        if (Math.Abs(chietkhau - chietkhaucu) > 5)
            return 0;
        return 1;
    }

}
